package ch.hearc.ig.android.crudpersonnes.communication;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;


import org.apache.http.util.ByteArrayBuffer;


import android.app.Activity;
import android.os.AsyncTask;
import android.util.Log;

public class ReadURL extends AsyncTask<String, Integer, String>{
	
	private Activity caller;
	
	public ReadURL(Activity a){
		caller=a;
	}
	@Override
	protected String doInBackground(String... params) {
		String string = "";
		URL url = null;
		BufferedInputStream bis=null;
		URLConnection connection = null;
		
		try{
			/** Exemple avec utilisation d'un proxy HTTP **/
			/*
			url = new URL(PersonneServiceCaller.appUrl.concat(params[0]));
			SocketAddress addr = new InetSocketAddress("proxy.he-arc.ch", 8080);
			Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
			connection = url.openConnection(proxy);
			*/
			/** Fin de l'exemple avec proxy**/
			
			/** Exemple sans proxy HTTP **/
			url = new URL(PersonneServiceCaller.appUrl.concat(params[0]));
			connection = url.openConnection();
			/** Fin de l'exemple sans proxy **/
			
			InputStream is = connection.getInputStream();
			bis = new BufferedInputStream(is);
			
			ByteArrayBuffer baf = new ByteArrayBuffer(50);
			int current = 0;
			while ((current = bis.read()) != -1) {
				baf.append((byte) current);
			}
			
			string = new String(baf.toByteArray());
			Log.i("OK R", string);
		}catch(Exception ex){
			string = ex.getMessage();
			Log.i("KO R", string);
		}finally{
			try {
				bis.close();
			} catch (IOException ex) {
				ex.printStackTrace();
				Log.v("error",ex.getMessage());
			}
		}
		
		return string;
	}
	/*
	 * (non-Javadoc)
	 * on j'ai pas compris ce qu'on fait lors de cette operation
	 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
	 */
	@Override
	protected void onPostExecute(String result) {
		Log.i("onPostExecute Method", result);
		Log.i("Caller !!!",caller.getClass().getName());
		
		
		
		if (caller.getClass().getName()=="lkadfjalk"){
			
		}
		else{
		}
		/*
        TextView txt = (TextView) caller.findViewById(R.id.personne_list);
        String newString = txt.getText() + "\n" + result;
        txt.setText(newString);
        */
    }
	

}
